using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using SBPweb.Themes.Windows;
using SBPweb.Themes.Windows.Skins;
using System.ComponentModel.Design;

namespace SBPweb.Controls.Windows.Common
{
	/// <summary>
	/// Class of Panel
	/// </summary>
	[ToolboxBitmap(typeof(System.Windows.Forms.Panel))]
	public partial class Panel : System.Windows.Forms.Panel
	{
		#region Private members

		protected bool mUseDefaultSkin = true;
		protected Skin mSkin;
		protected Border mBorder;
		protected Margin mContentMargin;
		protected Margin mSizingMargin;
		protected Margin mBorderThin;


		#endregion

		#region Constructor logic
		public int W;
		public Panel()
		{
			InitializeComponent();

			InitializeBorder();
			InitializeMargins();

			RegisterEventHandlers();

			SetStyle();

			this.UseDefaultSkin = true;
			W = this.DefaultSize.Width;

			this.ControlAdded += new ControlEventHandler(Panel_ControlAdded);

			//Host = (IDesignerHost)this.GetService(typeof(IDesignerHost));
		}

		

		void Panel_ControlAdded(object sender, ControlEventArgs e)
		{
			Point p = e.Control.Location;
			e.Control.LocationChanged += new EventHandler(Control_LocationChanged);
		}

		void Control_LocationChanged(object sender, EventArgs e)
		{
			Point p = ((Control)sender).Location;
		}
		
		public Panel(Skin skin): base()
		{
			mSkin = skin;

			InitializeComponent();
		}

		/*protected override void OnControlAdded(ControlEventArgs e)
		{
			base.OnControlAdded(e);
			IDesignerHost host = (IDesignerHost)this.GetService(typeof(IDesignerHost));
			DesignerTransaction dt = null;
			IComponentChangeService cService = (IComponentChangeService)GetService(typeof(IComponentChangeService));

			if (DesignMode & host != null)
			{
				dt = host.CreateTransaction("Draw panel control");
			}

			Control c = e.Control;

			if (host != null)
			{
				c = (Control)host.CreateComponent(e.Control.GetType());
			}

			//if (c != this.innerPanel)
			//{
			//    this.innerPanel.Controls.Add(c);

			//    //c.PointToClient(c.Location);
			//    //foreach (Control cc in this.innerPanel.Controls)
			//    //{
			//    //    Point p = cc.Location;
			//    //}

			//    if (c.Size.Width > contentWidth)
			//    {
			//        contentWidth = c.Size.Width;
			//    }
			//    if (c.Size.Height > contentHeight)
			//    {
			//        contentHeight = c.Size.Height;
			//    }
			//}
			int l = c.Left;
			int t = c.Top;
			//this.innerPanel.AutoScrollPosition = new Point(this.innerPanel.AutoScrollPosition.X + c.Location.X, this.innerPanel.AutoScrollPosition.Y + c.Location.Y);

			if (DesignMode && cService != null)
				cService.OnComponentChanging(this, null);

			if (DesignMode & dt != null)
			{
				dt.Commit();
			}

			Invalidate();
		}*/

		protected void InitializeBorder()
		{
			if (Skin == null)
			{
				mBorder = new Border();
				mBorderThin = new Margin(new Padding(1));
			}
			else
			{
				if (Skin.SelectSingleNode("Elements/GroupPanel/Border") != null)
				{
					mBorder = new Border(Skin.SelectSingleNode("Elements/GroupPanel/Border"));
				}
				if (Skin.SelectSingleNode("Elements/GroupPanel/Border/BorderThin") != null)
				{
					mBorderThin = new Margin(Skin.SelectSingleNode("Elements/GroupPanel/Border/BorderThin"));
				}
			}
		}

		protected void InitializeMargins()
		{
			if (Skin == null)
			{
				mContentMargin = new Margin();
				mSizingMargin = new Margin();
			}
			else
			{
				if (Skin.SelectSingleNode("Elements/GroupPanel/ContentMargins") != null)
				{
					mContentMargin = new Margin(Skin.SelectSingleNode("Elements/GroupPanel/ContentMargins"));
				}
				if (Skin.SelectSingleNode("Elements/GroupPanel/Image/SizingMargins") != null)
				{
					mSizingMargin = new Margin(Skin.SelectSingleNode("Elements/GroupPanel/Image/SizingMargins"));
				}
			}
		}

		protected void SetStyle()
		{
			base.SetStyle(ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint |ControlStyles.SupportsTransparentBackColor, true);
		}

		#endregion

		

		protected override System.Drawing.Size DefaultSize
		{
			get
			{
				return new System.Drawing.Size(W, base.DefaultSize.Height);
			}
		}

		#region Custom draw

		protected override void OnPaint(PaintEventArgs e)
		{
			if (Skin == null)
			{
				base.OnPaint(e);
				return;
			}
			
			base.OnPaintBackground(e);

			System.Drawing.Image img = Skin.CachedProperties.GroupPanelImage;

			if (img == null)
			{
				base.OnPaint(e);
				return;
			}

			int imgWidth = img.Width;
			int imgHeight = img.Height;
			int imgLeft = 0;
			int imgTop = 0;

			switch (this.BorderStyle)
			{
				default:
				case BorderStyle.FixedSingle:

					#region Image parts
					Rectangle iLT = new Rectangle(imgLeft, imgTop, mSizingMargin.Left, mSizingMargin.Top);
					Rectangle iT = new Rectangle(imgLeft + mSizingMargin.Left, imgTop, imgWidth - mSizingMargin.Horizontal, mSizingMargin.Top);
					Rectangle iRT = new Rectangle(imgLeft + imgWidth - mSizingMargin.Right, imgTop, mSizingMargin.Right, mSizingMargin.Top);
					Rectangle iLM = new Rectangle(imgLeft, imgTop + mSizingMargin.Top, mSizingMargin.Left, imgHeight - mSizingMargin.Vertical);
					Rectangle iM = new Rectangle(imgLeft + mSizingMargin.Left, imgTop + mSizingMargin.Top, imgWidth - mSizingMargin.Horizontal, imgHeight - mSizingMargin.Vertical);
					Rectangle iRM = new Rectangle(imgLeft + imgWidth - mSizingMargin.Right, imgTop + mSizingMargin.Top, mSizingMargin.Right, imgHeight - mSizingMargin.Vertical);
					Rectangle iLB = new Rectangle(imgLeft, imgTop + imgHeight - mSizingMargin.Bottom, mSizingMargin.Left, mSizingMargin.Bottom);
					Rectangle iB = new Rectangle(imgLeft + mSizingMargin.Left, imgTop + imgHeight - mSizingMargin.Bottom, imgWidth - mSizingMargin.Horizontal, mSizingMargin.Bottom);
					Rectangle iRB = new Rectangle(imgLeft + imgWidth - mSizingMargin.Right, imgTop + imgHeight - mSizingMargin.Bottom, mSizingMargin.Right, mSizingMargin.Bottom);
					#endregion

					#region Window parts
					Rectangle tLT = new Rectangle(this.ClientRectangle.Left, this.ClientRectangle.Top, mSizingMargin.Left, mSizingMargin.Top);
					Rectangle tT = new Rectangle(this.ClientRectangle.Left + mSizingMargin.Left, this.ClientRectangle.Top, this.ClientRectangle.Width - mSizingMargin.Horizontal, mSizingMargin.Top);
					Rectangle tRT = new Rectangle(this.ClientRectangle.Right - mSizingMargin.Right, this.ClientRectangle.Top, mSizingMargin.Right, mSizingMargin.Top);
					Rectangle tLM = new Rectangle(this.ClientRectangle.Left, this.ClientRectangle.Top + mSizingMargin.Top, mSizingMargin.Left, this.ClientRectangle.Height - mSizingMargin.Vertical);
					Rectangle tM = new Rectangle(this.ClientRectangle.Left + mSizingMargin.Left, this.ClientRectangle.Top + mSizingMargin.Top, this.ClientRectangle.Width - mSizingMargin.Horizontal, this.ClientRectangle.Height - mSizingMargin.Vertical);
					Rectangle tRM = new Rectangle(this.ClientRectangle.Right - mSizingMargin.Right, this.ClientRectangle.Top + mSizingMargin.Top, mSizingMargin.Right, this.ClientRectangle.Height - mSizingMargin.Vertical);
					Rectangle tLB = new Rectangle(this.ClientRectangle.Left, this.ClientRectangle.Bottom - mSizingMargin.Bottom, mSizingMargin.Left, mSizingMargin.Bottom);
					Rectangle tB = new Rectangle(this.ClientRectangle.Left + mSizingMargin.Left, this.ClientRectangle.Bottom - mSizingMargin.Bottom, this.ClientRectangle.Width - mSizingMargin.Horizontal, mSizingMargin.Bottom);
					Rectangle tRB = new Rectangle(this.ClientRectangle.Right - mSizingMargin.Right, this.ClientRectangle.Bottom - mSizingMargin.Bottom, mSizingMargin.Right, mSizingMargin.Bottom);
					#endregion

					#region Drawing
					e.Graphics.DrawImage(img, tLT, iLT, GraphicsUnit.Pixel);
					e.Graphics.DrawImage(img, tT, iT, GraphicsUnit.Pixel);
					e.Graphics.DrawImage(img, tRT, iRT, GraphicsUnit.Pixel);
					e.Graphics.DrawImage(img, tLM, iLM, GraphicsUnit.Pixel);
					e.Graphics.DrawImage(img, tM, iM, GraphicsUnit.Pixel);
					e.Graphics.DrawImage(img, tRM, iRM, GraphicsUnit.Pixel);
					e.Graphics.DrawImage(img, tLB, iLB, GraphicsUnit.Pixel);
					e.Graphics.DrawImage(img, tB, iB, GraphicsUnit.Pixel);
					e.Graphics.DrawImage(img, tRB, iRB, GraphicsUnit.Pixel);
					#endregion

					break;

				case BorderStyle.None:

					iM = new Rectangle(imgLeft + mSizingMargin.Left, imgTop + mSizingMargin.Top, imgWidth - mSizingMargin.Horizontal, imgHeight - mSizingMargin.Vertical);
					tM = new Rectangle(this.ClientRectangle.Left, this.ClientRectangle.Top, this.ClientRectangle.Width, this.ClientRectangle.Height);
					e.Graphics.DrawImage(img, tM, iM, GraphicsUnit.Pixel);

					break;
			}
		}

		#endregion

		protected void RegisterEventHandlers()
		{
		
			DefaultLookAndFeel.OnSkinChanged += new EventHandler(DefaultLookAndFeel_OnSkinChanged);
		}


		void DefaultLookAndFeel_OnSkinChanged(object sender, EventArgs e)
		{
			InitializeMargins();
			InitializeBorder();

			//this.BackColor = mBackColor.BackColor;
			this.Invalidate(true);
		}

		#region ISkinnable Members

		/// <summary>
		/// Use default skin?
		/// </summary>
		[Category("Look && feel")]
		public bool UseDefaultSkin
		{
			get
			{
				return mUseDefaultSkin;
			}
			set
			{
				mUseDefaultSkin = value;
				Invalidate();
			}
		}

		/// <summary>
		/// Used skin
		/// </summary>
		[Category("Look && feel")]
		public Skin Skin
		{
			get
			{
				if (this.UseDefaultSkin)
				{
					return DefaultLookAndFeel.Skin;
				}

				return mSkin;
			}
			set
			{
				mSkin = value;
				Invalidate();
			}
		}

		#endregion
	}
}
